//
//  WaterCollectionCtrl.m
//  UICollectionViewFlowLayoutTest
//
//  Created by Rocky on 16/4/15.
//  Copyright © 2016年 Rocky. All rights reserved.
//

#import "WaterCollectionCtrl.h"
#import "WaterFlowLayout.h"
#import "DataModel.h"
#import "WaterCollectionViewCell.h"
//
@interface WaterCollectionCtrl ()<UICollectionViewDelegate,WaterFlowLayoutDelegate>
{
    NSMutableArray *arrModel;
    
}
@property (nonatomic, strong) NSMutableDictionary *colunMaxYDic;

@end

@implementation WaterCollectionCtrl

static NSString * const reuseIdentifier = @"MyCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *file = [[NSBundle mainBundle] pathForResource:@"1.plist" ofType:nil];
    NSArray *keyValuesArray = [NSArray arrayWithContentsOfFile:file];
    self.colunMaxYDic =[ NSMutableDictionary dictionaryWithCapacity:1];
    //
    arrModel = [NSMutableArray array];
    for (NSDictionary *keyValues in keyValuesArray)
    {
        if (![keyValues isKindOfClass:[NSDictionary class]]) continue;
        DataModel *model = [[DataModel alloc]init];
        model.price = [keyValues valueForKey:@"price"];
        model.h = [[keyValues valueForKey:@"h"]integerValue];
        model.w = [[keyValues valueForKey:@"w"]integerValue];
        model.img = [keyValues valueForKey:@"img"];
        [arrModel addObject:model];
    }
    [self.collectionView reloadData];
    WaterFlowLayout* flowLayout = [[WaterFlowLayout alloc]init];
    flowLayout.delegate = self;
    self.collectionView.collectionViewLayout = flowLayout;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{

    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return arrModel.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    WaterCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    //cell.backgroundColor=[UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1.0f];;
    cell.backgroundColor= [UIColor greenColor];
    DataModel* data = arrModel[indexPath.row];
    [cell setCellData:data];
    //
    return cell;
}
//以分栏后的宽度为基准，宽高的按等缩放计算出高度。
-(CGFloat)getNewItemHeight:(NSIndexPath*)indexPath heightForWidth:(CGFloat)width
{
    DataModel* data = arrModel[indexPath.row];
    return data.h/data.w*width;
}
/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
